#!/usr/bin/env python3
import sys
import json
import requests

def main():
    # if len(sys.argv) < 2:
    #     print("错误: 未提供JSON数据", file=sys.stderr)
    #     sys.exit(1)

    # 获取命令行参数中的JSON字符串
    #     json_str = '{"after":{"productId":"P20230001","productName":"稳健增长混合型资产管理计划","riskLevel":"R4","managementFee":1.2,"status":"限制申购"},"before":{"productId":"P20230001","productName":"稳健增长混合型资产管理计划","riskLevel":"R3","managementFee":1.0,"status":"开放申购赎回"},"header":{"database":"ProductDB","messageId":"CDC-AM-20230615-001","operation":"UPDATE","source":"AssetManagementSystem","timestamp":"2023-06-15T15:45:30.789Z","transactionId":"TXN-20230615-12345"},"metadata":{"lsn":"LOG-20230615-0001","primaryKey":["productId"],"table":"product_info","timestamp":1686834330789}}'
    json_str = sys.stdin
    # print(json_str)
    # print(type(json_str))
    data = json.load(json_str)
# 1. 模拟你提供的完整数据
#     data = {
#                "actseq": "293.207",
#                "jstime": "1752737522705961",
#                "xid": "0x0007.00f.000024b4",
#                "op": 0,
#                "scn": "5715399815",
#                "scntime": 1580413154,
#                "owner": "TRADE",
#                "name": "TEST_PRD_BASE_INFO",
#                "optype": "INSERT",
#                "after": {
#                    "ID": "000004",
#                    "PROD_CODE": "全球配置混合B",
#                    "PROD_SHT": "混合型",
#                    "MANAGER": "刘敏",
#                    "FOUND_DT": "2019-01-15 00:00:00",
#                    "NET_VALUE": "0.975",
#                    "TOTAL_VALUE": "210000000",
#                    "RISK_LEVEL": "中等",
#                    "CURRENCY": "USD",
#                    "STATUS": "正常"
#                },
#                "after_cached": [],
#                "after_key": {
#                    "ID": "000004"
#                },
#                "rowid": "AAAfJpAAFAAAGE8AAD",
#                "sync": 1,
#                "sdbtype": "oracle"
#            }
    # 打印JSON数据（美化输出）
    # print(json.dumps(data, indent=2, ensure_ascii=False))

    # 在这里可以添加你的业务逻辑
    process_asset_management_data(data)


def process_asset_management_data(data):
    # 2. 构造符合接口要求的结构（将 data 字段作为字符串，table 单独提取）
    after = data["after"]
    if isinstance(after, dict):
        after = [after]  # 包装成列表
    # 转换为字段值数组
    data_rows = [list(item.values()) for item in after]

    payload = {
        "moduleId": "1945376308043501569",
        "parameter": {
            "data": json.dumps(data_rows, ensure_ascii=False),
            "table": data["name"]
        }
    }

    # 3. 发送 POST 请求
    response = requests.post(
        url="http://10.150.8.18:8910/py-land",
        headers={
            "uid": "1",
            "Content-Type": "application/json"
        },
        data=json.dumps(payload)
    )

    # 4. 打印结果
    print("Status Code:", response.status_code)
    print("Response:", response.text)


if __name__ == "__main__":
    main()
